package cfca.seal.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ImageUtil
{
  public static boolean isPng(InputStream is)
  {
    try
    {
      byte[] pngData = new byte[4];
      is.read(pngData);
      if ((pngData[1] == 80) && (pngData[2] == 78) && (pngData[3] == 71)) {
        return true;
      }
      is.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("图片必须为.png格式");
    return false;
  }

  public static boolean isPng(byte[] pngData) {
    if ((pngData[1] == 80) && (pngData[2] == 78) && (pngData[3] == 71)) {
      return true;
    }
    System.out.println("图片必须为.png格式");
    return false;
  }

  public static void converterImage(File imgFile, String format, File formatFile) throws IOException {
    BufferedImage bIMG = ImageIO.read(imgFile);
    ImageIO.write(bIMG, format, formatFile);
  }

  public static void createSealImage(String drawStr, int width, int height, Integer fontHeight, String sealImagePath) throws IOException {
    if (fontHeight == null) {
      fontHeight = Integer.valueOf(10);
    }
    BufferedImage buffImg = new BufferedImage(width, height, 1);
    Graphics2D gd = buffImg.createGraphics();

    buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, 3);
    gd = buffImg.createGraphics();

    FontRenderContext context = gd.getFontRenderContext();
    Font font = new Font("微软雅黑", 0, fontHeight.intValue());
    Rectangle2D bounds = font.getStringBounds(drawStr, context);
    double y = (height - bounds.getHeight()) / 2.0D;
    double ascent = -bounds.getY();
    double baseY = y + ascent;
    gd.setFont(font);
    gd.setColor(Color.RED);
    gd.drawRect(0, 0, width - 1, height - 1);
    gd.drawString(drawStr, width / 2 - fontHeight.intValue() * drawStr.length() / 2, (int)baseY);
    ImageIO.write(buffImg, "png", new File(sealImagePath));
  }

  public static byte[] transferAlpha(Image image)
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      ImageIcon imageIcon = new ImageIcon(image);
      BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), 6);
      Graphics2D g2D = (Graphics2D)bufferedImage.getGraphics();
      g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
      int alpha = 0;
      for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
        for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
          int rgb = bufferedImage.getRGB(j2, j1);

          int R = (rgb & 0xFF0000) >> 16;
          int G = (rgb & 0xFF00) >> 8;
          int B = rgb & 0xFF;
          if ((255 - R < 30) && (255 - G < 30) && (255 - B < 30)) {
            rgb = alpha + 1 << 24 | rgb & 0xFFFFFF;
          }

          bufferedImage.setRGB(j2, j1, rgb);
        }

      }

      g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());

      ImageIO.write(bufferedImage, "png", byteArrayOutputStream); } catch (Exception e) {
      e = 
        e; } finally {
    }return byteArrayOutputStream.toByteArray();
  }

  public static byte[] transferAlpha(byte[] imageByte)
  {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      ImageIcon imageIcon = new ImageIcon(imageByte);
      BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), 6);
      Graphics2D g2D = (Graphics2D)bufferedImage.getGraphics();
      g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
      int alpha = 0;
      for (int j1 = bufferedImage.getMinY(); j1 < bufferedImage.getHeight(); j1++) {
        for (int j2 = bufferedImage.getMinX(); j2 < bufferedImage.getWidth(); j2++) {
          int rgb = bufferedImage.getRGB(j2, j1);

          int R = (rgb & 0xFF0000) >> 16;
          int G = (rgb & 0xFF00) >> 8;
          int B = rgb & 0xFF;
          if ((255 - R < 30) && (255 - G < 30) && (255 - B < 30)) {
            rgb = alpha + 1 << 24 | rgb & 0xFFFFFF;
          }

          bufferedImage.setRGB(j2, j1, rgb);
        }

      }

      g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());

      ImageIO.write(bufferedImage, "png", byteArrayOutputStream); } catch (Exception e) {
      e = 
        e; } finally {
    }return byteArrayOutputStream.toByteArray();
  }

  public static byte[] resizeImage(byte[] imageByte, int width, int height)
  {
    ByteArrayInputStream bais = new ByteArrayInputStream(imageByte);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
      Image srcImg = ImageIO.read(bais);
      BufferedImage buffImg = null;
      buffImg = new BufferedImage(width, height, 6);
      buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, 4), 0, 0, null);
      ImageIO.write(buffImg, "png", byteArrayOutputStream);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return byteArrayOutputStream.toByteArray();
  }
}